package com.yoga.service.impl;

import com.yoga.entity.*;
import com.yoga.mapper.*;
import com.yoga.service.YogaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class YogaUserServiceImpl implements YogaUserService {
    @Autowired
    private YogaTransactionDao yogaTransactionDao;
    @Autowired
    private YogaMymoneyDao yogaMymoneyDao;
    @Autowired
    private YogaMyorderDao yogaMyorderDao;
    @Autowired
    private UserAppealDao userAppealDao;
    @Autowired
    private YogaCoachDao yogaCoachDao;
    @Autowired
    private YogaUserDao yogaUserDao;
    @Autowired
    private YogaVenueDao yogaVenueDao;


    //充值交易记录
    @Override
    public boolean saveTransaction(String uid, String type, BigDecimal money) {
        String uuid = String.valueOf(UUID.randomUUID());
        YogaTransaction save = new YogaTransaction(null,uid,new Date(),type,"收入",money,"充值",uuid);
        //保存记录表
        yogaTransactionDao.insert(save);
        //查找金额表
        YogaMymoney yogaMymoney = yogaMymoneyDao.selectByPrimaryKey(Integer.parseInt(uid));
        //如果没有创建
        if(yogaMymoney == null){
            YogaMymoney yogaMymoney1 = new YogaMymoney(Integer.parseInt(uid),"000000",money);
            yogaMymoneyDao.insert(yogaMymoney1);
        }else {//否则追加金额
            BigDecimal oldMoney = yogaMymoney.getMoney();
            BigDecimal newMoney = oldMoney.add(money);
            YogaMymoney yogaMymoney1 = new YogaMymoney(Integer.parseInt(uid),null,newMoney);
            yogaMymoneyDao.updateByPrimaryKeySelective(yogaMymoney1);
        }
        return true;
    }

    //扣款
    @Override
    public boolean updataMoney(Integer oid, BigDecimal money, Integer uid, String statese) {
        //用户确认支付后后修改到订单表
        Integer IntegerMoney = money.intValue();
        YogaMyorder yogaMyorder = new YogaMyorder(oid, null, null,
                IntegerMoney, null, new Date(), statese);
        yogaMyorderDao.updateByPrimaryKeySelective(yogaMyorder);
        //查出用户向谁发出的私教请求,并找出教练的课时费
        YogaMyorder yogaMyorder1 = yogaMyorderDao.selectByPrimaryKey(oid);
        Integer coachUid = Integer.valueOf(yogaMyorder1.getGetuid());
        YogaCoach yogaCoach = yogaCoachDao.selectByPrimaryKey(coachUid);
        Integer coachMoney = yogaCoach.getClassmoney();
//      ===============================================================
        //查找用户金额表
        YogaMymoney yogaMymoney = yogaMymoneyDao.selectByPrimaryKey(uid);
        //获取用户现有的钱
        BigDecimal money1 = yogaMymoney.getMoney();
        //减少用户的钱
        BigDecimal newMoney = money1.subtract(money);
        //修改用户的金额
        YogaMymoney yogaMymoney1 = new YogaMymoney(uid, null, newMoney);
        yogaMymoneyDao.updateByPrimaryKeySelective(yogaMymoney1);
        //增加超管的钱和教练的钱

        //从用户支付的金额中减去教练的课时费,剩下的给超管
        BigDecimal rootMoney = money.subtract(BigDecimal.valueOf(coachMoney));

        //超管的===========
        YogaMymoney rootYogaMymoney = yogaMymoneyDao.selectByPrimaryKey(3);
        BigDecimal rootOldMoney = rootYogaMymoney.getMoney();
        BigDecimal rootNewMoney = rootOldMoney.add(rootMoney);
        YogaMymoney rootYogaMymoney1 = new YogaMymoney(3, null, rootNewMoney);
        yogaMymoneyDao.updateByPrimaryKeySelective(rootYogaMymoney1);
        //==============

        //教练的===============
        YogaMymoney CoachYogaMymoney1 = new YogaMymoney(coachUid, null, BigDecimal.valueOf(coachMoney));
        yogaMymoneyDao.updateByPrimaryKeySelective(CoachYogaMymoney1);
        //==================

//      ===============================================================
        //保存记录表
        String uuid = String.valueOf(UUID.randomUUID());
        YogaTransaction save = new YogaTransaction(null,String.valueOf(uid),new Date(),"线上","支出",money,"付款课程",uuid);
        yogaTransactionDao.insert(save);
        return true;
    }


    //申诉添加
    @Override
    public boolean saveAppeal(UserAppeal userAppeal) {
        System.out.println("=============");
        System.out.println(userAppeal);
        System.out.println("=============");
        return userAppealDao.insertSelective(userAppeal) >= 1 ? true : false;
    }

    //场馆的教练
    @Override
    public List<YogaUser> findAll(Integer vid) {
        YogaCoachExample example = new YogaCoachExample();
        example.createCriteria().andVidEqualTo(vid);
        List<YogaCoach> list = yogaCoachDao.selectByExample(example);
        List<Integer> uid = new ArrayList<>();
        for (YogaCoach y : list) {
            uid.add(y.getUid());
        }
        YogaUserExample example1 = new YogaUserExample();
        example1.createCriteria().andUidIn(uid);
        return yogaUserDao.selectByExample(example1);
    }

    //场馆评分
    @Override
    public boolean positive(YogaVenue yogaVenue) {
        return yogaVenueDao.updateByPrimaryKeySelective(yogaVenue) >= 1 ? true : false;
    }

    //修改user表
    @Override
    public boolean updataUser(YogaUser yogaUser) {
        yogaUserDao.updateByPrimaryKeySelective(yogaUser);
        return true;
    }
    //个人钱包
    @Override
    public YogaMymoney personMoney(Integer uid) {
        return yogaMymoneyDao.selectByPrimaryKey(uid);
    }
    //查一个,返回最新的数据
    @Override
    public YogaUser findOne(Integer uid) {

        return yogaUserDao.selectByPrimaryKey(uid);

    }
    //查出所有教练
    @Override
    public List<YogaCoach> findAllCoach() {

        return yogaCoachDao.selectByExample(null);

    }

    @Override
    public List<YogaMyorder> coachByPrivate(Integer coachuid) {
        YogaMyorderExample example = new YogaMyorderExample();
        example.createCriteria().andGetuidEqualTo(String.valueOf(coachuid));
        return yogaMyorderDao.selectByExample(example);
    }

    @Override
    public List<YogaMyorder> userByPrivate(String uid) {
        YogaMyorderExample example = new YogaMyorderExample();
        example.createCriteria().andPayuidEqualTo(uid);
        return yogaMyorderDao.selectByExample(example);
    }

    @Override
    public Boolean addUser(YogaUser yogaUser) {
        int i = yogaUserDao.insert(yogaUser);
        return i>0?true:false;
    }

    @Override
    public YogaUser findUserByPhone(String tel) {
        YogaUserExample example = new YogaUserExample();
        example.createCriteria().andPhoneEqualTo(tel);
        List<YogaUser> userList = yogaUserDao.selectByExample(example);
        return userList.size()>0?userList.get(0):null;
    }
}
